package b1.project.common.config;

import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.keygen.KeyGeneratorFactory;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

/**
 * mybatis-flex的一些配置
 *
 * @author sun
 * @date 2024/9/27 17:11
 */
@Configuration
public class MyBatisFlexConfiguration implements MyBatisFlexCustomizer {
    private static final Logger logger = LoggerFactory.getLogger("mybatis-flex-sql");

    /***
     * 将 SQL 打印到日志中
     */
    public MyBatisFlexConfiguration() {
        //开启审计功能
        AuditManager.setAuditEnable(true);

        //设置 SQL 审计收集器
        AuditManager.setMessageCollector(auditMessage ->
                logger.info("{},{}ms", auditMessage.getFullSql()
                        , auditMessage.getElapsedTime())
        );
    }

    /**
     * @param globalConfig 全局配置
     */
    @Override
    public void customize(FlexGlobalConfig globalConfig) {
        //注册自定义UUID主键
        KeyGeneratorFactory.register("myUUID", new UUIDKeyGenerator());

        //全局乐观锁配置
        FlexGlobalConfig.getDefaultConfig().setVersionColumn("version");
    }

}
